//
//  DYMeishiTuijiancaiCell.swift
//  SheYangBigData
//
//  Created by 宋海胜 on 2021/1/11.
//  Copyright © 2021 叁拾叁. All rights reserved.
//



import UIKit

class DYMeishiTuijiancaiCell: SYBaseCell , UICollectionViewDelegate, UICollectionViewDataSource{
    
    var models: [DYCateListModel] = []
    var collectionView: UICollectionView!
    

    var bgView: UIView = {
        let v: UIView = UIView()
        v.backgroundColor = UIColor(hex: 0x367FF6)
        v.layer.cornerRadius = 2
        return v
    }()
    
    var sepView: UIView = {
        let v: UIView = UIView()
        v.backgroundColor = Constant.bgViewColor
        return v
    }()
    
    let nameLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x1E1E1E)
        v.font = 17.autoBoldFontSize()
        v.text = "推荐菜"
        return v
    }()
    

    var paiming: VGNoteView = {
        let v: VGNoteView = VGNoteView()
        v.width = SCREEN_WIDTH - 60
        v.backgroundColor = UIColor(hex: 0xFFF8E8)
//        v.height = 40
        return v
    }()

    public func configureView(_ models: [DYCateListModel]) {
        self.models = models
        let titles:[String] = models.map { $0.name ?? "" }
        let paimingHeight = paiming.initViews(texts: titles, singlelLine: false)
        paiming.snp.remakeConstraints { (make) in
            make.top.equalTo(collectionView.snp.bottom).offset(15.0.autoWidth())
            make.left.equalToSuperview().offset(autoWidth(15.0))
            make.right.equalToSuperview().offset(autoWidth(-15.0))
            make.height.equalTo(paimingHeight)
            make.bottom.equalToSuperview().offset(-22.autoWidth())
        }
        self.collectionView.reloadData()
    }
    
    override func initViews() {
        

        setCollectionView()
        contentView.addSubview(nameLb)
        contentView.addSubview(bgView)
        
        contentView.addSubview(collectionView)
        contentView.addSubview(paiming)
        contentView.addSubview(sepView)
        bgView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(16.0.autoWidth())
            make.left.equalToSuperview().offset(autoWidth(14.5))
            make.width.equalTo(4.0.autoWidth())
            make.height.equalTo(15.0.autoWidth())
        }
        nameLb.snp.makeConstraints { (make) in
            make.centerY.equalTo(bgView)
            make.left.equalTo(bgView.snp.right).offset(autoWidth(7.0))
            make.right.equalToSuperview().offset(autoWidth(-15.0))
            make.height.equalTo(16.0.autoWidth())
        }
        collectionView.snp.makeConstraints { (make) in
            make.top.equalTo(nameLb.snp.bottom).offset(autoWidth(15.0))
            make.left.right.equalToSuperview()
            make.height.equalTo(125.0.autoWidth())
        }

        paiming.snp.makeConstraints { (make) in
            make.top.equalTo(collectionView.snp.bottom).offset(15.0.autoWidth())
            make.left.equalToSuperview().offset(autoWidth(15.0))
            make.right.equalToSuperview().offset(autoWidth(-15.0))
            make.height.equalTo(0)
            make.bottom.equalToSuperview().offset(-22.autoWidth())
        }
        sepView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.height.equalTo(10.0.autoWidth())
            make.bottom.equalToSuperview()
        }
    }
    
    func setCollectionView() {
        
            
        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        let itemWidth = 145.0
        layout.itemSize = CGSize(width: itemWidth, height: 125)
//        layout.minimumLineSpacing = 10
//        layout.minimumInteritemSpacing = 10
        layout.scrollDirection = .horizontal
        collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
        collectionView.backgroundColor = .white
        collectionView.showsHorizontalScrollIndicator = false
        
        
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(DYJingquYoulanSubCell.self, forCellWithReuseIdentifier: NSStringFromClass(DYJingquYoulanSubCell.self))
        self.addSubview(self.collectionView)
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return models.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell: DYJingquYoulanSubCell? = collectionView.dequeueReusableCell(withReuseIdentifier: NSStringFromClass(DYJingquYoulanSubCell.self), for: indexPath) as? DYJingquYoulanSubCell
        cell?.configure(self.models[indexPath.row])
        return cell!
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
//        let vc = DYJingquJianjieViewController()
//        self.currentVC.navigationController?.pushViewController(vc, animated: true)
    }
    
    
    @objc func chakanAction() {
        let vc = DYJingQuListViewController(id: "1")
        self.currentVC.navigationController?.pushViewController(vc, animated: true)
    }
}

